//
//  CarView.m
//  car
//
//  Created by chenxu on 15/3/18.
//  Copyright (c) 2015年 chenxu. All rights reserved.
//

#import "CarView.h"
#import "Input_LeftImage_Cell.h"
#import "Input_LeftRightLabel_Cell.h"
#import "DayPicker_CellTableViewCell.h"
#import "NetAPIManager.h"
#import "car-Swift.h"
#define kCellIdentifier_carList @"Input_LeftImage_Cell"
#define kCellIdentifier_day @"Input_day_Cell"

@interface CarView()
{
    BOOL select1,select2;
}
@property (nonatomic , copy) infochange infochangeblock;
@property (nonatomic , copy) endinput endinputblock;
//@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic, strong) Car* car;


@end

@implementation CarView



- (id)initWithFrame:(CGRect)frame car:(Car *)car{
    
    self = [super initWithFrame:frame];
    if (self) {
    
        if(car){
            _car = car;
        }
        else{
            _car = [[Car alloc]init];
        }
        _myTableView = ({
            UITableView *tableView = [[UITableView alloc] initWithFrame:self.bounds style:UITableViewStyleGrouped];
            tableView.backgroundColor = kColorTableBG;
            tableView.delegate = self;
            tableView.dataSource = self;
            [tableView registerClass:[Input_LeftImage_Cell class] forCellReuseIdentifier:kCellIdentifier_carList];
            [tableView registerClass:[DayPicker_CellTableViewCell class] forCellReuseIdentifier:kCellIdentifier_day];
            //tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
            [self addSubview:tableView];
            tableView;
        });
        
        titles = @[@"车牌号码:",@"厂牌型号:",@"车辆车款:",@"发动机号:",@"车架号:"];
        placeholders = @[@"暂时只支持天津车牌",@"例:上海大众 朗逸",@"例:2009品雅版",@"请参照行车证填写",@"请参照行车证填写"];
    }
    return self;

}


#pragma mark - table view
#pragma mark Table Data Source Methods
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section ==0) {
        return 5;
    }
    else{
        return 3;
    }
}

#pragma mark Table Delegate Methods
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 20;
}
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (indexPath.section == 0) {
        return 40;
    }
    else{
        if (indexPath.row == 0 || indexPath.row == 2) {
            if ([cell isKindOfClass:[DVDatePickerTableViewCell class]]) {
                DVDatePickerTableViewCell* cell = (DVDatePickerTableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
                return [cell datePickerHeight];
            }
        }
        return 40;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (indexPath.section == 1) {
        if ([cell isKindOfClass:[DVDatePickerTableViewCell class]]) {
            DVDatePickerTableViewCell* cell = (DVDatePickerTableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
            [cell selectedInTableView:tableView];
        }
    }
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
    
    NSString *left = [titles objectAtIndex:indexPath.row];
    NSString *holder = [placeholders objectAtIndex:indexPath.row];
    NSString *value;
    if (_car) {
        switch (indexPath.row) {
            case 0:
                value = _car.Carid;
                break;
            case 1:
                value = _car.Carpinpai;
                break;
            case 2:
                value = _car.Carxinghao;
                break;
            case 3:
                value = _car.Carfadongjiid;
                break;
            case 4:
                value = _car.Carchejiahao;
                break;
           
            default:
                break;
        }
    }

        Input_LeftRightLabel_Cell *cell = [[[NSBundle mainBundle] loadNibNamed:@"Input_LeftRightLabel_Cell" owner:self options:nil] firstObject];

        if (indexPath.row==6) {
            [cell configWithLeftLabel:left andRightLabel:@"万元" andPlaceholder:holder andValue:value ];
        }
        else{
            [cell configWithLeftLabel:left andRightLabel:nil andPlaceholder:holder andValue:value ];
        }
    
        cell.endinputBlock = ^(){
            [UIView down:self];
        };
        switch (indexPath.row) {
            case 0:
            {
                cell.textValueChangedBlock = ^(NSString* str){
                    self.car.Carid = str;
                };
                break;
            }
                
            case 1:
            {
                cell.textValueChangedBlock = ^(NSString* str){
                    self.car.Carpinpai = str;
                };
                break;
            }
                
            case 2:
            {
                cell.textValueChangedBlock = ^(NSString* str){
                    self.car.Carxinghao = str;
                };
                break;
            }
            case 3:
            {
                cell.textValueChangedBlock = ^(NSString* str){
                    self.car.Carfadongjiid = str;
                };
                break;
            }
            case 4:
            {
                cell.textValueChangedBlock = ^(NSString* str){
                    self.car.Carchejiahao = str;
                    [UIView up:self length:150];
                };
                break;
            }
        }
   
        
        return cell;
    }
    
    //下半section
    else{
        if (indexPath.row == 0) {
            DVDatePickerTableViewCell *cell = [DVDatePickerTableViewCell new];
            [cell setTitle:@"注册登记日期:"];
            
            //不为空设置为已有的
            if (![self.car.Carbuydate isEqualToString:@""]) {
                NSDate *time = [NSDate dateFromString:self.car.Carbuydate withFormat:@"yyyy年MM月dd日"];
                [cell setNewDate:time];
                
            }
            //空则设置为今天
            else{
                self.car.Carbuydate = [cell.date stringWithFormat:@"yyyy年MM月dd日"];
            }
            
            cell.selectblock = ^(NSDate* date){
                self.car.Carbuydate = [date stringWithFormat:@"yyyy年MM月dd日"];
                NSLog(@"更新了！%@",self.car.Carbuydate);
            };
            return cell;
        }
                
        if (indexPath.row == 1) {
        
                NSString *CellIdentifier = @"Input_LeftRightLabel_Cell";
                Input_LeftRightLabel_Cell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
                if (!cell) {
                    cell = [[[NSBundle mainBundle] loadNibNamed:@"Input_LeftRightLabel_Cell" owner:self options:nil] firstObject];
                }
                cell.endinputBlock = ^(){
                    [UIView down:self];
                };
                cell.tapInsideBlock = ^(){
                    [UIView up:self length:150];
                };
                [cell configWithLeftLabel:@"购车价格:" andRightLabel:@"万元" andPlaceholder:@"购车价格" andValue:_car.Carprice ];
                cell.textValueChangedBlock = ^(NSString* str){
                    self.car.Carprice = str;
                };
                return cell;

        }
        if (indexPath.row == 2) {
            DVDatePickerTableViewCell *cell = [DVDatePickerTableViewCell new];
            [cell setTitle:@"保险到期日期:"];
            //不为空设置为已有的
            if (![self.car.Carbaoxiandaoqi isEqualToString:@""]) {
                NSDate *time = [NSDate dateFromString:self.car.Carbaoxiandaoqi withFormat:@"yyyy年MM月dd日"];
                [cell setNewDate:time];
                
            }
            //空则设置为今天
            else{
                self.car.Carbaoxiandaoqi = [cell.date stringWithFormat:@"yyyy年MM月dd日"];
            }
            
            cell.selectblock = ^(NSDate* date){
                self.car.Carbaoxiandaoqi = [date stringWithFormat:@"yyyy年MM月dd日"];
                NSLog(@"更新了！%@",self.car.Carbaoxiandaoqi);
            };
            return cell;
        }
    }
    return nil;
    
    
}


- (void)addcar:(void (^)())block{
    NSString  *tip = [_car tips];
    if (tip) {
        [self showHudTipStr:tip];
    }
    else{
        [[NetAPIManager sharedManager] add_car:[_car toAddParams] andBlock:^(id data, NSError *error) {
            BOOL success = [[data valueForKeyPath:@"Success"]boolValue];
            NSString *msg = [data valueForKeyPath:@"Msg"];
            NSString *Cid = [data valueForKeyPath:@"Backdata.Cid"];
            if (success) {
                [self showHudTipStr:@"添加成功"];
                
                //读取该车信息
                [[NetAPIManager sharedManager] request_cars:[[Login curLoginUser] tousernameParams] andBlock:^(id data, NSError *error) {
                    if (data) {
                        for (int i=0; i< [data count]; i++) {
                            Car* car = [NSObject objectOfClass:@"Car" fromJSON:[data objectAtIndex:i]];
                            if ([car.Cid isEqual:Cid]) {
                                [Login selectCar:car];
                            }
                        }
                    }
                }];
                
                block();
            }else{
                [self showHudTipStr:msg];
            }
        }];
    }
}

- (void)modifycar:(void (^)())block{
    NSString  *tip = [_car tips];
    if (tip) {
        [self showHudTipStr:tip];
    }
    else{
        [[NetAPIManager sharedManager] modify_car:[_car toModifyParams] andBlock:^(id data, NSError *error) {
            block();
            if (data) {
                [self showHudTipStr:@"修改成功"];
            }else{
                [self showHudTipStr:@"修改失败"];
            }
        }];
    }
    
}

- (void)deletecar:(void (^)())block{
    NSString  *tip = [_car tips];
    if (tip) {
        [self showHudTipStr:tip];
    }
    else{
        [[NetAPIManager sharedManager] delete_car:[_car toDeleteParams] andBlock:^(id data, NSError *error) {
            block();
            if (data) {
                [self showHudTipStr:@"删除成功"];
            }else{
                [self showHudTipStr:@"删除失败"];
            }
        }];
    }
    
}

@end
